//
//  SearchShopViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/15.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "SearchShopViewController.h"
#import "CollectTableViewCell.h"
#import "GoodListViewController.h"

@interface SearchShopViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;

@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;
Assign int pstart;

@end

//网络
@implementation SearchShopViewController (NetWorking)

- (void)getData:(NSString *)pstart{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"s_name":self.name,
                                 @"pstart":pstart,
                                 @"psize":@"20"};
    [[AFAppDotNetAPIClient sharedClient] POST:@"StoreMobile/selectLike.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            if ([pstart isEqualToString:@"0"]) {
                [self.dataArray removeAllObjects];
                self.pstart = 0;
            }
            self.pstart ++;
            [self.dataArray addObjectsFromArray:responseObject[@"obj"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

@implementation SearchShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"店铺";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CollectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CollectTableViewCell class])];
    cell.dataDictionary = self.dataArray[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodListViewController *vc = [GoodListViewController new];
    vc.auId = self.dataArray[indexPath.section][@"s_id"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 5;
        _tableView.sectionHeaderHeight = .01;
        _tableView.rowHeight = 72;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[CollectTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CollectTableViewCell class])];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                [self getData:@"0"];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
           [self getData:[NSString stringWithFormat:@"%d",self.pstart]];
        }];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

- (void)setName:(NSString *)name{
    _name = name;
    [self getData:@"0"];
}
@end
